/*
 * @# editAlbum.js 2011-12-28 上午11:07:58
 * 
 * Copyright (C) 2010 - 2011 广州羿安信息科技有限公司
 * Guangzhou ianswer information technology co. ltd.
 * 
 * All rights reserved!
 */

var EdibAlbumDialog = {
	_haveInited : false,
	_$dialog : $('#dialog-edit-album'),
	_createType : false,
	_createCallback : false,
	_editCallback : false,
	_init : function() {
		this._haveInited = true;
		
		var self = this;
		this._$dialog.dialog({
			modal : true,
			title : '编辑专辑',
			autoOpen : false,
			width : 600,
			height : 450,
			resizable : false
		});
		$('#dialog-album-cancel').click(function(e) {
			self.close();
			return false;
		});
		$('#dialog-album-submit').click(function(e) {
			self.save();
			return false;
		});
	},
	
	edit : function(albumId, callback) {
		var self = this;
		this._editCallback = callback;
		$.getJSON(window.ctx + '/albums/' + albumId + '/edit', function(album) {
			$('#dialog-album-id').val(album.id);
			$('#dialog-album-name').val(album.name);
			$('#dialog-album-description').val(album.description);
			$('#dialog-album-tags').val(album.tags);
			$('#dialog-album-gallery option[value="' + album.galleryId + '"]').prop('selected', true);
			
			self._open();
		});
	},
	
	createShows : function(callback) {
		this._create('shows', callback);
	},
	createDress : function(callback) {
		this._create('dress', callback);
	},
	
	_create : function(type, callback) {
		this._createType = type;
		this._createCallback = callback;
		
		$('#dialog-album-id').val('');
		$('#dialog-album-name').val('');
		$('#dialog-album-description').val('');
		$('#dialog-album-tags').val('');
		
		this._open();
	},
	
	_open : function() {
		if (!this._haveInited) {
			this._init();
		}
		this._$dialog.dialog('open');
	},
	
	close : function() {
		this._$dialog.dialog('close');
	},
	
	save : function() {
		var albumId = $('#dialog-album-id').val();
		if (albumId) {
			this._update(albumId);
		} else {
			this._insert();
		}
	},
	
	_insert : function() {
		var self = this,
			album = this._fetchData(),
			url = window.ctx + '/albums/shows/create';
		if (this._createType == 'dress') {
			url = window.ctx + '/albums/dress/create';
		}
		$.post(url, album, function(feedback) {
			if (feedback.successful) {
				album['id'] = feedback.obj;
				self._createCallback && self._createCallback(album);
				self.close();
			}
		});
	},
	
	_fetchData : function() {
		var albumId = $('#dialog-album-id').val(),
			name = $('#dialog-album-name').val(),
			description = $('#dialog-album-description').val(),
			tags = $('#dialog-album-tags').val(),
			galleryId = $('#dialog-album-gallery').val();
		return {
			id : albumId,
			name : name,
			galleryId : galleryId,
			description : description,
			tags : tags
		};
	},
	
	_update : function(albumId) {
		var self = this;
		var album = this._fetchData();
		$.post(window.ctx + '/albums/' + albumId + '/edit', album, function(feedback) {
			if (feedback.successful) {
				self._editCallback && self._editCallback(album);
				self.close();
			}
		});
	}
};
